<template>
	<view class="login-container">
		<view class="login-item">
			<text>账号:</text>
			<input type="text" @input="onAccount" placeholder="添加账号" />
		</view>
		<view class="login-item">
			<text>密码:</text>
			<input class="uni-input" password type="text" placeholder="请输入密码" @input="onPassword" />
		</view>
		<view>
			<button type="primary" @click="getRegister">注册</button>
		</view>
	</view>
</template>

<script>
	import sqlite from '../../utils/sqlite_test.js'
	import md5 from 'js-md5';
	import {
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				account: '',
				password: ''
			};
		},
		methods: {
			...mapMutations('m_user', ['updateUserinfo']),
			//获取账号信息
			onAccount(event) {
				this.account = event.detail.value
				console.log(this.account)
			},
			//获取密码信息
			onPassword(event) {
				let password = event.detail.value
				this.password = md5(password)
				console.log(this.password)
			},
			//注册账号信息
			async getRegister() {
				if (this.account == '') {
					uni.$showMsg('账号不能为空')
					return
				}
				if (this.password == '') {
					uni.$showMsg('密码不能为空')
					return
				}
				let openExist = await sqlite.OpenExist()
				if (openExist.code == 1) {
					let selectSql = "select * from Login where account='" + this.account + "'"
					let result = await sqlite.selectSQL(selectSql)
					if (result.code == 1) {
						if (result.data != null) {
							uni.$showMsg('账号已存在')
						}
					} else {
						let sql = [
							'create table if not exists Login(id INTEGER PRIMARY KEY,account varchar(20),password varchar(20),nickname varchar(20))',
							"insert into Login(account,password) values('" + this.account + "','" + this.password +
							"')"
						]
						let res = await sqlite.executeSQL(sql)
						if (res.code == 1) {
							let result = await sqlite.selectSQL("select * from Login where account='" + this.account +
								"'")
							if (result.code == 1) {
								this.updateUserinfo(result.data[0])
								uni.switchTab({
									url: '/pages/bill/bill'
								})
							}
						}
					}

				}
			}
		}
	}
</script>

<style lang="scss">
	.login-container {
		margin: 10px 20px 0px 20px; //上 左 下 右

		.login-item {
			display: flex;
			flex-direction: row;
			margin-bottom: 10px;
		}
	}
</style>